// pages/vault/vault.js
const { mpserverless } = getApp();
Page({
  data: {
    showSave: false,      //存钱弹出框
    showWithdraw: false,   //取钱弹出框
    saveMoney: '',         //存钱金额
    goMoney: '',           //取钱金额
    goRadio: '消费',            //钱取哪儿
    saveRadio: '银行卡',       //钱存在哪里
    changeDetail: [],         //接收存取明细
    headDetail: {},            //存放总金额及总存取
  },


  //弹框确认
  saveMoney(event) {
    if (this.data.saveMoney == '') {
      wx.showToast({
        title: '请输入金额！',
        icon: 'none',
        duration: 1500,
      }).then(e => {
        this.setData({ showSave: true });
      });
    } else {
      let currentTime = new Date().getTime();
      currentTime = this.timestampToTime(currentTime);  //日期格式转换
      //转化数字
      let num = new Number(this.data.saveMoney);
      let money = num.toFixed(2);
      const openid = getApp().globalData.openId;
      mpserverless.db.collection('vault').insertOne({
        openId: openid,
        changeDate: currentTime,
        isSave: true,
        changeMoney: money,
        changePlace: this.data.saveRadio
      }).then(res => {
        if (res.success == true) {
          wx.showToast({
            title: '小金库增加拉！',
            icon: 'none',
            duration: 1500,
          });
          this.onShow();
        } else {
          wx.showToast({
            title: '出现问题！',
            icon: 'none',
            duration: 1500,
          });
        }
      }).catch(console.error);
    }
  },
  withdrawMoney(event) {
    if (this.data.goMoney == '') {
      wx.showToast({
        title: '请输入金额！',
        icon: 'none',
        duration: 1500,
      }).then(e => {
        this.setData({ showWithdraw: true });
      });
    } else {
      let currentTime = new Date().getTime();
      currentTime = this.timestampToTime(currentTime);  //日期格式转换
      //转化数字
      let num = new Number(this.data.goMoney);
      let money = num.toFixed(2);
      const openid = getApp().globalData.openId;
      mpserverless.db.collection('vault').insertOne({
        openId: openid,
        changeDate: currentTime,
        isSave: false,
        changeMoney: money,
        changePlace: this.data.goRadio
      }).then(res => {
        if (res.success == true) {
          wx.showToast({
            title: '小金库又变瘦拉！',
            icon: 'none',
            duration: 1500,
          });
          this.onShow();
        } else {
          wx.showToast({
            title: '出现问题！',
            icon: 'none',
            duration: 1500,
          });
        }
      }).catch(console.error);
    }
  },
  //打开弹窗
  openSaveMoney() {
    this.setData({ showSave: true });
  },
  openWithdrawMoney() {
    this.setData({ showWithdraw: true });
  },
  //存取金额
  setSaveMoney(event) {
    this.setData({ saveMoney: event.detail });
  },
  setGoMoney(event) {
    this.setData({ goMoney: event.detail });
  },
  //途径选择radio
  saveChange(e) {
    this.setData({ saveRadio: e.detail });
  },
  goChange(e) {
    this.setData({ goRadio: e.detail });
  },
  onClose() {
    this.setData({ showSave: false, showWithdraw: false });
  },
  timestampToTime(time) {
    //时间戳转换
    // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
    var date = new Date(time);
    let y = date.getFullYear();
    let MM = date.getMonth() + 1;
    let d = date.getDate();
    let h = date.getHours();
    let m = date.getMinutes();
    let s = date.getSeconds();
    MM = MM < 10 ? "0" + MM : MM;
    d = d < 10 ? "0" + d : d;
    h = h < 10 ? "0" + h : h;
    m = m < 10 ? "0" + m : m;
    s = s < 10 ? "0" + s : s;
    return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let openid = getApp().globalData.openId;
    // 获取所有存取明细
    mpserverless.db.collection('vault')
      .find({ 'openId': openid }).then(res => {
        if (res.success) {
          let arr = res.result;
          let head = {};  // 临时对象
          let total = 0;  //总金额
          let saveTotal = 0;  //总存入
          let goTotal = 0;   //总取出
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].isSave) {
              saveTotal += Number(arr[i].changeMoney);
            } else {
              goTotal += Number(arr[i].changeMoney);
            }
          }
          total = saveTotal - goTotal;
          head.total = total;
          head.saveTotal = saveTotal;
          head.goTotal = goTotal;

          this.setData({
            changeDetail: arr,
            headDetail: head
          });
        } else {
          wx.showToast({
            title: '出错拉！',
            icon: 'none',
            duration: 1500,
          });
        }
      }).catch(console.error);

  },

})